<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="97px"
      @submit.native.prevent
    >
      <el-form-item label="增材方式名称" prop="technologyAddMethodName">
        <el-input
          v-model="queryParams.technologyAddMethodName"
          placeholder="请输入增材方式名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['technology:materialAddMethod:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['technology:materialAddMethod:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['technology:materialAddMethod:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['technology:materialAddMethod:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
    :cell-style="{padding:this.tablePadding}"
      v-loading="loading"
      :data="wayList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="增材方式id"
        align="center"
        prop="technologyMaterialAddMethodId"
      />
      <el-table-column
        label="增材方式名称"
        align="center"
        prop="technologyAddMethodName"
      />
      <el-table-column
        label="增材方式缩写"
        align="center"
        prop="technologyAddMethodAbbreviation"
      />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                :active-value="0"
                :inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
      </el-table-column>
       <el-table-column label="建立人" align="center" prop="createBy" />
      <el-table-column label="建立时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['technology:materialAddMethod:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['technology:materialAddMethod:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改增材方式列对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="增材方式名称" prop="technologyAddMethodName">
          <el-input
            v-model="form.technologyAddMethodName"
            placeholder="请输入增材方式名称"
          />
        </el-form-item>
        <el-form-item
          label="增材方式简称"
          prop="technologyAddMethodAbbreviation"
        >
          <el-input
            v-model="form.technologyAddMethodAbbreviation"
            placeholder="增材方式缩写"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWay,
  getWay,
  delWay,
  addWay,
  updateWay,
  changeWayStatus
} from "@/api/base/way";

export default {
  name: "Way",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 增材方式列表格数据
      wayList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      statusOptions:[],
      selectNames:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        technologyAddMethodName: null,
        technologyAddMethodAbbreviation: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        technologyAddMethodName: [
          { required: true, message: "增材方式名称不能为空", trigger: "blur" },
        ],
        technologyAddMethodAbbreviation: [
          { required: true, message: "增材方式简称不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "建立人不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "建立时间不能为空", trigger: "blur" },
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === 0 ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.technologyAddMethodName + '"的增材方式吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",closeOnClickModal:false,
          type: "warning"
        }).then(function() {
          return changeWayStatus(row.technologyMaterialAddMethodId, row.status);
        }).then(() => {
          this.msgSuccess(text + "成功");
        }).catch(function() {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    /** 查询增材方式列列表 */
    getList() {
      this.loading = true;
      listWay(this.queryParams).then((response) => {
        this.wayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        technologyMaterialAddMethodId: null,
        technologyAddMethodName: null,
        technologyAddMethodAbbreviation: null,
        remark: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.technologyMaterialAddMethodId);
      this.selectNames = selection.map((item) => item.technologyAddMethodName);
     this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加增材方式";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const technologyMaterialAddMethodId =
        row.technologyMaterialAddMethodId || this.ids;
      getWay(technologyMaterialAddMethodId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改增材方式";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.technologyMaterialAddMethodId != null) {
            updateWay(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWay(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const technologyMaterialAddMethodIds =
        row.technologyMaterialAddMethodId || this.ids;
      const name=row.technologyAddMethodName||this.selectNames
      this.$confirm(
        '是否确认删除增材方式名称为"' +
          name +
          '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",closeOnClickModal:false,
          type: "warning",
        }
      )
        .then(function () {
          return delWay(technologyMaterialAddMethodIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "technology/materialAddMethod/export",
        {
          ...this.queryParams,
        },
        `technology_materialAddMethod.xlsx`
      );
    },
  },
};
</script>